﻿using RM.Weighing.Models;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace RM.Weighing
{
    internal static class Program
    {

        public static string labelPrint = "";
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            #region GUID 程序运行唯一
            bool createdNew;
            Guid ownGUID = new Guid(((GuidAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(GuidAttribute))).Value);
            Mutex instance = new Mutex(true, ownGUID.ToString("N"), out createdNew);
            if (!createdNew)
            {
                MessageBox.Show("只能运行唯一实例");
                Process.GetCurrentProcess().Kill();
            }
            #endregion

            #region 注册与否
            object regisData = Tools.GetSystemInfo.GetRegistData("Weighing");
            if (regisData == null)
            {
                UIRegister register = new UIRegister();
                if (register.ShowDialog() != DialogResult.OK)
                {
                    Process.GetCurrentProcess().Kill();
                }
            }
            else
            {
                string a = regisData.ToString();
                if (!a.Equals(new Tools().GetRegistrationCode(new Tools().ObtainMachineCode())))
                {
                    UIRegister register = new UIRegister();
                    if (register.ShowDialog() != DialogResult.OK)
                    {
                        Process.GetCurrentProcess().Kill();
                    }
                }

            }
            #endregion

            IniSettings.Current.Path = System.IO.Directory.GetCurrentDirectory();
            IniSettings.Current.Save();
            IniSettings.Current.Load();
            labelPrint = File.ReadAllText(IniSettings.Current.Path + "\\label.txt", Encoding.UTF8);


            try
            {
                Application.Run(new UIMain());
            }
            catch (Exception e)
            {

                new UIMain().ShowErrorDialog(e.Message);
            }
        }
    }
}
